package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-17
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //登录
    @Override
    public String loin(UcenterMember member) {
    //获取手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();
        //比较是否输入手机号和密码
        if (StringUtils.isEmpty(member)|| StringUtils.isEmpty(password)){
            throw  new GuliException(20001,"登录失败，账号或者密码为空！");
        }
        //进行手机号的验证
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember moblieMenber = baseMapper.selectOne(wrapper);
        if (moblieMenber==null){
            throw new GuliException(20001,"登录失败，手机号输入错误！");
        }
        //进行密码的判断
        //因为存入数据库的密码必定是经过加密
        //所以输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5
        if (!MD5.encrypt(password).equals(moblieMenber.getPassword())){
            throw new GuliException(20001,"登录失败，密码输入错误！");
        }
        //判断是否被禁
        if (moblieMenber.getIsDisabled()){
            throw new GuliException(20001,"登录失败，账号已经被禁用！");
        }
        //如果登录成功，那就生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(moblieMenber.getId(), moblieMenber.getNickname());
        return jwtToken;
    }
    //注册
    @Override
    public void register(RegisterVo registerVo) {

        //获取注册的数据
        String nickname = registerVo.getNickname();//获取昵称
        String mobile = registerVo.getMobile();//获取手机号
        String password = registerVo.getPassword();//获取密码
        String code = registerVo.getCode();//获取验证码
        //判断输入数据是否为空
        if (StringUtils.isEmpty(nickname)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)||StringUtils.isEmpty(code)){
            throw new GuliException(20001,"输入数据为空，注册失败！");
        }
        //判断验证码是否输入正确
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new GuliException(20001,"验证码输入错误！");
        }
        //判断手机号是否重复登录
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count>0){
            throw new GuliException(20001,"手机账号已经注册过，不能继续注册！");
        }
        //判断昵称是否重复
        wrapper.eq("nickname",nickname);
        Integer count1=baseMapper.selectCount(wrapper);
        if (count1>1){
            throw new GuliException(20001,"账号昵称已经存在，用户名不能重复！");
        }
   //把注册的数据加入数据库
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setNickname(nickname);
        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);//不禁用账户，可以使用
        ucenterMember.setAvatar("https://online-teach-file.oss-cn-beijing.aliyuncs.com/teacher/2019/10/30/de47ee9b-7fec-43c5-8173-13c5f7f689b2.png");
        baseMapper.insert(ucenterMember);

    }
    //判断在数据库是否有相同的微信id，有就无需添加
    @Override
    public UcenterMember getOpenIdMenber(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }
    //查询某一天注册人数
    @Override
    public Integer countRegisterDay(String day) {

        return baseMapper.countRegisterDay(day);
    }
}
